﻿CREATE PROCEDURE [RestartFramework].[PutETLJobReset]
		@pETLJobName		SYSNAME
,		@pETLJobHistoryID	INT     =   -1
,		@pServerExecutionId	BIGINT  =   -1
,       @pPrint             BIT     =   1
AS
BEGIN
		SET NOCOUNT ON;
		DECLARE  @vRowCount  INT = 0;
		UPDATE	[RestartFramework].[ETLJobStage]
		SET		[ETLJobStageIsComplete]	=	CAST(0 AS BIT)
		WHERE	[ETLJobId]	=	(
									SELECT	ej.[ETLJobId]
									FROM	[RestartFramework].[ETLJob] ej
									WHERE	ej.[ETLJobName]	=	@pETLJobName
								);
		SET @vRowCount = @@ROWCOUNT;
		IF(@pPrint = 1)
						PRINT   CONVERT(NVARCHAR(5),@vRowCount) + N' rows in [RestartFramework].[ETLJobStage] reset ([ETLJobIsComplete]=0) for ETLJobName=' + 
										@pETLJobName;

		UPDATE	[RestartFramework].[ETLJobHistory]
		SET		[ETLJobHistoryEndTime]		=	SYSDATETIMEOFFSET()
		,		[EndedByServerExecutionId]	=	@pServerExecutionId
		WHERE	[ETLJobHistoryId] = @pETLJobHistoryID;
		SET @vRowCount = @@ROWCOUNT;
		IF(@pPrint = 1)
						PRINT   CONVERT(NVARCHAR(5),@vRowCount) + N' rows in [RestartFramework].[ETLJobHistory] set to "ended" for ETLJobHistoryId=' + 
										CONVERT(NVARCHAR(10),@pETLJobHistoryID);
END